package com.qf.controller;

import com.alibaba.fastjson.JSONObject;
import com.qf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * JSON处理  类
 * @author lixu
 */
//@Controller
@RequestMapping("/json")
//@ResponseBody
@RestController
public class JsonController {

    /**
     * 测试
     *  接收JSON格式的字符串  转成POJO
     *
     * GET http://localhost:8080/json/test1
     *   请求体中
     *   user={id:123,name:xixi}
     *
     *   ### Send POST request with body as parameters
     * POST http://localhost:8080/user
     * Content-Type: application/x-www-form-urlencoded
     *
     *  json={id:123,name:xixi}
     *
     *       由同学们完成
     *       //{id:123,name:xixi}
     *         //User user = JSONObject.parseObject(json, User.class);
     *         //User user1 = objectMapper.readValue(json, User.class);
     */
    @RequestMapping(value = "/test1",method = RequestMethod.POST)
    //@ResponseBody //POJO对象 转成JSON串
    //public @ResponseBody User test1(@RequestBody User user){
    public User test1(@RequestBody(required = false) User user){
        System.out.println(user);
        //处理对象
        user.setId(000);
        user.setName("ooo");
        user.setPassword("987");
        return user;
    }
}
